﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using MM.DBUtility;

namespace MobileManager
{
    public partial class MmaskManage : System.Web.UI.Page
    {
        private MM.Model.T_Users userModel;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (Session["CurUser"] == null)
                {
                    Response.Redirect("Login.aspx");
                    return;
                }
                else
                {
                    userModel = (MM.Model.T_Users)Session["CurUser"];
                    if (userModel.RoleID.Equals("3"))
                        return;
                }
                if (!IsPostBack)
                {
                    BindDdlChannel(ref ddlChannel);
                    ddlChannel.Items.Insert(0, new ListItem("全部", "-1"));

                    GVDataBindAll();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        private void BindDdlChannel(ref DropDownList ddl)
        {
            string strSQL = "SELECT ChannelNO,ChannelNO+'-'+ ChannelNumber+'-'+ChannelOrder as Name FROM T_Channel WITH(NOLOCK) order by ChannelNO";

            ddl.DataSource = DbHelperSql.Query(strSQL).Tables[0];
            ddl.DataValueField = "ChannelNO";
            ddl.DataTextField = "Name";
            ddl.DataBind();
        }

        protected void btnADD_Click(object sender, EventArgs e)
        {
            try
            {
                string strNumber = txtNumber.Text.Trim(), strChinese = txtChinese.Text.Trim(), strEnglish = txtEnglish.Text.Trim();
                if (string.IsNullOrEmpty(strNumber) && string.IsNullOrEmpty(strChinese) && string.IsNullOrEmpty(strEnglish))
                {
                    MM.Common.Common.PageStartAlert(this.Page, "关键字请至少填写一项！");
                    return;
                }
                string strChannelNO = ddlChannel.SelectedValue;
                if (strChannelNO.Equals("-1"))
                {
                    MM.Common.Common.PageStartAlert(this.Page, "请选择通道编号！");
                    return;
                }

                string strSQL = string.Format("INSERT INTO T_Mask(TelNumber,Chinese,English,ChannelNO) VALUES('{0}',N'{1}','{2}','{3}')", strNumber, strChinese, strEnglish, strChannelNO);
                MM.DBUtility.DbHelperSql.ExecuteSql(strSQL);
                MM.DBUtility.BLSqlData.InsertLogs(userModel.UserID, string.Format("添加了短信屏蔽信息：{0},{1},{2},{3}。", strNumber, strChinese, strEnglish, strChannelNO), "短信屏蔽管理");
                MM.Common.Common.PageStartAlert(this.Page, "添加成功！");
                GVDataBindAll();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            try
            {
                GVDataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void btnQueryAll_Click(object sender, EventArgs e)
        {
            try
            {
                GVDataBindAll();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #region GV info
        private void GVDataBind()
        {
            string strSQL = "SELECT a.ID,a.TelNumber,a.Chinese,a.English,a.ChannelNO,b.ChannelNO+'-'+ b.ChannelNumber+'-'+b.ChannelOrder as ChannelName FROM T_Mask a,T_Channel b WITH(NOLOCK) WHERE a.ChannelNO=b.ChannelNO ";

            if (!"-1".Equals(ddlChannel.SelectedValue))
                strSQL += string.Format(" AND a.ChannelNO='{0}'", ddlChannel.SelectedValue);
            if (!string.IsNullOrEmpty(txtNumber.Text.Trim()))
                strSQL += string.Format(" AND a.TelNumber LIKE '%{0}%'", txtNumber.Text.Trim());
            if (!string.IsNullOrEmpty(txtChinese.Text.Trim()))
                strSQL += string.Format(" AND a.Chinese LIKE N'%{0}%'", txtChinese.Text.Trim());
            if (!string.IsNullOrEmpty(txtEnglish.Text.Trim()))
                strSQL += string.Format(" AND a.English LIKE '%{0}%'", txtEnglish.Text.Trim());
            strSQL += " ORDER BY a.CreateTime DESC";

            DataSet ds = DbHelperSql.Query(strSQL);
            GVinfo.DataSource = ds;
            GVinfo.DataBind();
            ViewState["DataTemp"] = ds;
        }
        private void GVDataBindAll()
        {
            DataSet ds = DbHelperSql.Query("SELECT a.ID,a.TelNumber,a.Chinese,a.English,a.ChannelNO,b.ChannelNO+'-'+ b.ChannelNumber+'-'+b.ChannelOrder as ChannelName FROM T_Mask a,T_Channel b WITH(NOLOCK) WHERE a.ChannelNO=b.ChannelNO ORDER BY a.CreateTime DESC");
            GVinfo.DataSource = ds;
            GVinfo.DataBind();
            ViewState["DataTemp"] = ds;
        }
        protected void GVinfo_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            try
            {
                GVinfo.EditIndex = -1;
                GVinfo.DataSource = (DataSet)ViewState["DataTemp"];
                GVinfo.DataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void GVinfo_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowState == DataControlRowState.Edit || e.Row.RowState == (DataControlRowState.Alternate | DataControlRowState.Edit))
            {
                DropDownList ddl = (DropDownList)e.Row.FindControl("ddlChannel");
                BindDdlChannel(ref ddl);
                ddl.SelectedValue = ((HiddenField)e.Row.FindControl("HFChannelNO")).Value;
            }
        }

        protected void GVinfo_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                string strID = ((HiddenField)GVinfo.Rows[e.RowIndex].FindControl("HF2")).Value;
                DbHelperSql.ExecuteSql(string.Format("DELETE FROM T_Mask WHERE ID={0}", strID));
                GVinfo.DataSource = (DataSet)ViewState["DataTemp"];
                GVinfo.DataBind();
                MM.DBUtility.BLSqlData.InsertLogs(userModel.UserID, string.Format("删除了短信屏蔽信息：序号={0}。", strID), "短信屏蔽管理");
                MM.Common.Common.PageStartAlert(this.Page, "删除成功！");
                GVDataBindAll();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void GVinfo_RowEditing(object sender, GridViewEditEventArgs e)
        {
            try
            {
                GVinfo.EditIndex = e.NewEditIndex;
                GVinfo.DataSource = (DataSet)ViewState["DataTemp"];
                GVinfo.DataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void GVinfo_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            try
            {
                string strChannelNO = ((DropDownList)GVinfo.Rows[e.RowIndex].FindControl("ddlChannel")).SelectedValue;
                string strNumber = ((TextBox)GVinfo.Rows[e.RowIndex].FindControl("txtNumber")).Text.Trim();
                string strChinese = ((TextBox)GVinfo.Rows[e.RowIndex].FindControl("txtChinese")).Text.Trim();
                string strEnglish = ((TextBox)GVinfo.Rows[e.RowIndex].FindControl("txtEnglish")).Text.Trim();
                if (string.IsNullOrEmpty(strNumber) && string.IsNullOrEmpty(strChinese) && string.IsNullOrEmpty(strEnglish))
                {
                    MM.Common.Common.PageStartAlert(this.Page, "请至少填写一项！");
                    return;
                }

                string strID = ((HiddenField)GVinfo.Rows[e.RowIndex].FindControl("HF1")).Value;
                MM.DBUtility.DbHelperSql.ExecuteSql(string.Format("UPDATE T_Mask SET TelNumber='{0}',Chinese=N'{1}',English='{2}',ChannelNO='{3}' WHERE ID={4}", strNumber, strChinese, strEnglish, strChannelNO, strID));
                MM.DBUtility.BLSqlData.InsertLogs(userModel.UserID, string.Format("更新了短信屏蔽信息：序号={0}。", strID), "短信屏蔽管理");
                MM.Common.Common.PageStartAlert(this.Page, "更新成功！");

                GVinfo.EditIndex = -1;
                GVDataBindAll();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void GVinfo_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GVinfo.PageIndex = e.NewPageIndex;
            GVinfo.DataSource = (DataSet)ViewState["DataTemp"];
            GVinfo.DataBind();
        }
        #endregion
    }
}
